using System;
using System.Collections.Generic;
using UnityEngine;
using Voodoo.Sauce.Core;
using Voodoo.Sauce.Internal.DebugScreen;
using Voodoo.Tune.Core;

namespace Voodoo.Sauce.Debugger
{
	public class EnvironmentDebugScreen : Screen, IConditionalScreen
	{
		private const string NEW_SERVER_MESSAGE = "Your {0} server is set to {1}, restart the application to apply the modifications.";

		private const string NEW_VOODOO_TUNE_VERSION_MESSAGE = "Your VoodooTune version is set to {0}, restart the application to apply the modifications.";

		[SerializeField]
		private DebugPopup _debugPopup;

		private readonly Dictionary<EnvironmentSettings.Server, string> _server = new Dictionary<EnvironmentSettings.Server, string>
		{
			{
				EnvironmentSettings.Server.Tech,
				"Production"
			},
			{
				EnvironmentSettings.Server.Staging,
				"Staging"
			},
			{
				EnvironmentSettings.Server.Dev,
				"Development"
			}
		};

		private Server _voodooTuneServer;

		private Dictionary<Server, DebugToggleButton> _vtToggle = new Dictionary<Server, DebugToggleButton>();

		private Status _voodooTuneVersion;

		private Dictionary<Status, DebugToggleButton> _versionToggle = new Dictionary<Status, DebugToggleButton>();

		private EnvironmentSettings.Server _analyticsServer;

		private Dictionary<EnvironmentSettings.Server, DebugToggleButton> _analyticsToggle = new Dictionary<EnvironmentSettings.Server, DebugToggleButton>();

		public bool CanDisplay
		{
			get
			{
				VoodooSettings voodooSettings = VoodooSettings.Load();
				if (!voodooSettings.UseVoodooAnalytics)
				{
					return voodooSettings.UseRemoteConfig;
				}
				return true;
			}
		}

		private string VtNewVersion => $"Your VoodooTune version is set to {VoodooTunePersistentData.SavedStatusToName[_voodooTuneVersion]}, restart the application to apply the modifications.";

		private void Start()
		{
			VoodooSettings settings = VoodooSettings.Load();
			InitProxyServerSection(settings);
			InitVoodooTune(settings);
			InitAnalytics(settings);
		}

		private void InitProxyServerSection(VoodooSettings settings)
		{
			if (settings.UseVoodooAnalytics)
			{
				InputField("Proxy Server", "Enter proxy server...", EnvironmentSettings.SaveProxyServer, EnvironmentSettings.GetProxyServer);
			}
		}

		private void InitVoodooTune(VoodooSettings settings)
		{
			if (settings.UseRemoteConfig)
			{
				OpenFoldout("VoodooTune");
				InitVoodooTuneServerButtons();
				InitVoodooTuneVersionButtons();
				CloseFoldout();
			}
		}

		private void InitVoodooTuneServerButtons()
		{
			_voodooTuneServer = VoodooTunePersistentData.SavedServer;
			foreach (Server value in Enum.GetValues(typeof(Server)))
			{
				string label = VoodooTunePersistentData.SavedServerDisplayNames[(int)value];
				_vtToggle.Add(value, Toggle(label, value == _voodooTuneServer, delegate(bool isOn)
				{
					VoodooTuneServerSwitch(isOn, value);
				}));
			}
			Label("Changing the server will affect remote config and remote config debugger.");
		}

		private void InitVoodooTuneVersionButtons()
		{
			_voodooTuneVersion = VoodooTunePersistentData.SavedStatus;
			for (int i = 0; i < 2; i++)
			{
				Status status = (Status)i;
				string label = VoodooTunePersistentData.SavedStatusToName[status];
				_versionToggle.Add(status, Toggle(label, status == _voodooTuneVersion, delegate(bool isOn)
				{
					VoodooTuneSwitchVersion(isOn, status);
				}));
			}
			Label("Changing the version will affect the remote config debugger.");
		}

		private void VoodooTuneServerSwitch(bool isOn, Server server)
		{
			if (server == _voodooTuneServer || !isOn)
			{
				if (server == _voodooTuneServer && !isOn)
				{
					_vtToggle[server].SetValue(isOn: true);
				}
				return;
			}
			Server voodooTuneServer = _voodooTuneServer;
			_voodooTuneServer = server;
			_vtToggle[voodooTuneServer].SetValue(isOn: false);
			_vtToggle[server].SetValue(isOn: true);
			VoodooTunePersistentData.SavedServer = server;
			string arg = VoodooTunePersistentData.SavedStatusToName[_voodooTuneVersion];
			string message = $"Your {arg} server is set to {_voodooTuneServer}, restart the application to apply the modifications.";
			DisplayPopup(message);
		}

		private void VoodooTuneSwitchVersion(bool isOn, Status version)
		{
			if (version == _voodooTuneVersion || !isOn)
			{
				if (version == _voodooTuneVersion && !isOn)
				{
					_versionToggle[version].SetValue(isOn: true);
				}
				return;
			}
			Status voodooTuneVersion = _voodooTuneVersion;
			_voodooTuneVersion = version;
			_versionToggle[voodooTuneVersion].SetValue(isOn: false);
			_versionToggle[version].SetValue(isOn: true);
			VoodooTunePersistentData.SavedStatus = version;
			DisplayPopup(VtNewVersion);
		}

		private void InitAnalytics(VoodooSettings settings)
		{
			if (settings.UseVoodooAnalytics)
			{
				OpenFoldout("Analytics");
				InitAnalyticsServerButtons();
				CloseFoldout();
			}
		}

		private void InitAnalyticsServerButtons()
		{
			_analyticsServer = EnvironmentSettings.GetAnalyticsServer();
			foreach (EnvironmentSettings.Server value in Enum.GetValues(typeof(EnvironmentSettings.Server)))
			{
				_analyticsToggle.Add(value, Toggle(_server[value], value == _analyticsServer, delegate(bool isOn)
				{
					AnalyticsServerSwitch(isOn, value);
				}));
			}
			Label("Changing the server will affect analytics.");
		}

		private void AnalyticsServerSwitch(bool isOn, EnvironmentSettings.Server server)
		{
			if (server == _analyticsServer || !isOn)
			{
				if (server == _analyticsServer && !isOn)
				{
					_analyticsToggle[server].SetValue(isOn: true);
				}
				return;
			}
			EnvironmentSettings.Server analyticsServer = _analyticsServer;
			_analyticsServer = server;
			_analyticsToggle[analyticsServer].SetValue(isOn: false);
			_analyticsToggle[server].SetValue(isOn: true);
			EnvironmentSettings.SaveAnalyticsServer(server);
			string message = $"Your {_server[server].ToLower()} server is set to {_analyticsServer}, restart the application to apply the modifications.";
			DisplayPopup(message);
		}

		private void DisplayPopup(string message)
		{
			_debugPopup.Initialize(message, delegate
			{
				_debugPopup.gameObject.SetActive(value: false);
			});
			_debugPopup.gameObject.SetActive(value: true);
		}
	}
}
